define(['hy5b/graph/index'], function graph(graphUtil) {

function getBoundingBox(points, smoothConstraint) {
  var ptMin = [Infinity, Infinity];
  var ptMax = [-Infinity, -Infinity];

  if (smoothConstraint) {
    for (var i = 0; i < points.length; i++) {
      var pt = points[i];

      if (pt[0] < ptMin[0]) {
        ptMin[0] = pt[0];
      }

      if (pt[1] < ptMin[1]) {
        ptMin[1] = pt[1];
      }

      if (pt[0] > ptMax[0]) {
        ptMax[0] = pt[0];
      }

      if (pt[1] > ptMax[1]) {
        ptMax[1] = pt[1];
      }
    }
  }

  return {
    min: smoothConstraint ? ptMin : ptMax,
    max: smoothConstraint ? ptMax : ptMin
  };
}


function brightPointCanvas(hex, size) {
  hex = hex || '#ffffff';
  size = size || 128;
  let canvas = document.createElement('canvas')
  canvas.width = size;
  canvas.height = size;
  var ctx = canvas.getContext('2d');
  let gradient = ctx.createRadialGradient(
    canvas.width / 2,
    canvas.height / 2,
    canvas.width / 16,
    canvas.width / 2,
    canvas.height / 2,
    canvas.width / 2
  );
  var color = graphUtil.splitHex(hex);
  gradient.addColorStop(0.0, '#ffffff');
  gradient.addColorStop(0.1, '#ffffff');
  gradient.addColorStop(0.5, graphUtil.joinRgb(color, 0.5))
  gradient.addColorStop(0.7, graphUtil.joinRgb(color, 0.3))
  gradient.addColorStop(0.9, graphUtil.joinRgb(color, 0.1))
  gradient.addColorStop(1.0, graphUtil.joinRgb(color, 0))
  ctx.fillStyle = gradient;
  ctx.beginPath();
  ctx.arc(
    canvas.width / 2,
    canvas.height / 2,
    canvas.width / 2,
    0,
    Math.PI * 2,
    true
  )
  ctx.fill();

  return canvas;
}

function drawHalo(lc, x, y, r, color) {
  lc.draw(ctx => {
    const gradient = ctx.createRadialGradient(x, y, 0, x, y, r);
    gradient.addColorStop(0, color);
    const colorArr = graphUtil.parse2Color(color)
    colorArr[3] = 0
    gradient.addColorStop(1, graphUtil.joinRgb(colorArr));
    ctx.fillStyle = gradient
    ctx.arc(x, y, r, 0, 2 * Math.PI)
    ctx.fill()
  })
}

function drawParticalLightLine(lc, points, color, r) {
  const len = points.length - 1;
  const colorArr = graphUtil.parse2Color(color)
  _.forEach(points, (point, i) => {
    //固定30个点最后5个为白
    const fillStyle = Number(colorArr[3]) === 0 ? 'rgba(0,0,0,0)' : graphUtil.joinRgb(colorArr, 0.8 * i / len);
    const arcRadius = 0.8 * (r + 1) * (i === len ? (i - 2) : i) / len
    lc.draw(ctx => {
      ctx.shadowColor = '#fff';
      ctx.shadowBlur = 4;
      ctx.fillStyle = fillStyle
      ctx.arc(point[0], point[1], arcRadius, 0, 2 * Math.PI, false)
      ctx.fill();
    })
  })
}

/**
 * r 外径
 * width 圆弧宽度，width == r时不镂空
 * n 分段数，算上盖半叠加会产生2n-1段
 */
function drawConicGradientArc(lc, x, y, r, startAngle, endAngle, colors, width, n) {
  const count = 2 * n - 1;

}

return { getBoundingBox, brightPointCanvas, drawHalo, drawParticalLightLine }

})
